//
//  LSZCollectionVC.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/19.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZCollectionVC.h"

@interface LSZCollectionVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableViewCollection;
@property(nonatomic,strong) NSMutableArray * arratMut;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,assign) NSInteger indexNum;
@end

@implementation LSZCollectionVC
-(void)getCollectionData:(NSInteger)page
{
    [LSZRequestDicData initWithCollection:page :^(BOOL isOK, NSString * string, LSZArticleDetailsManager * collectionMg) {
        if(isOK == YES){
            if(page == 0){
                [self.arratMut removeAllObjects];
                 [self.arratMut addObjectsFromArray:collectionMg.mutMgArray];
            }else{
                [self.arratMut addObjectsFromArray:collectionMg.mutMgArray];
            }
            [self.tableViewCollection reloadData];
        }else{
            if ([string isEqualToString:@"重复登录"]) {
                [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZCollectionVC"];
            }else{
                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
            }
        }
    }];
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
-(void)addNotificationCenter
{
    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZCollectionVC"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 0;
    [self addNotificationCenter];
    [self getCollectionData:self.page];
    self.arratMut = [NSMutableArray array];
    self.navigationItem.title = @"我的收藏";
    [self setCollectionUi];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arratMut.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZDocumentdetailsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZDocumentdetailsCell" forIndexPath:indexPath];
    if (self.arratMut.count > indexPath.row) {
        LSZArticleDetailsMg * mg = nil;
        mg = self.arratMut[indexPath.row];
        cell.articleDetailsMg = mg;
        UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(lpGR:)];
        //设定最小的长按时间 按不够这个时间不响应手势
        longPressGR.minimumPressDuration=0.7;
        [cell addGestureRecognizer:longPressGR];
    }
  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma mark -实现手势对应的功能
-(void)lpGR:(UILongPressGestureRecognizer *)lpGR
{
    if (lpGR.state == UIGestureRecognizerStateBegan) {//手势开始
        CGPoint point = [lpGR locationInView:self.tableViewCollection];
        NSIndexPath*indexPath =  [self.tableViewCollection indexPathForRowAtPoint:point];
        if(indexPath !=nil){
            self.indexNum = indexPath.row;
            NSLog(@"点击的是第%ld行",(long)self.indexNum);
        }
    }
    if (lpGR.state == UIGestureRecognizerStateEnded){
        //手势结束
        [self longPressDelete];//添加弹窗
    }
}
-(void)longPressDelete
{
    __weak typeof(self) weakSelf = self;
    ACActionSheet *actionSheet = [[ACActionSheet alloc] initWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@[@"删除"] actionSheetBlock:^(NSInteger buttonIndex) {
        if(buttonIndex ==0) {
            [weakSelf initWithDeleteTheCollection];
        }
    }];
    [actionSheet show];
}
#pragma mark - 删除
-(void)initWithDeleteTheCollection
{
    LSZArticleDetailsMg * mg = nil;
    mg = self.arratMut[self.indexNum];
    [LSZRequestData initWithDeleteTheCollection:mg.id_id :^(BOOL isOk, NSString * string, NSString * str) {
        if(isOk == YES ){
            [self initWithtring:@"数据删除成功!"];
            [self.arratMut removeObjectAtIndex:self.indexNum];
            [self.tableViewCollection reloadData];
        }
    }];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.arratMut.count > indexPath.row) {
        LSZArticleDetailsMg * mg = nil;
        mg = self.arratMut[indexPath.row];
        LSZDirectoryDetailsViewController * vc = [[LSZDirectoryDetailsViewController alloc]init];
        vc.url = mg.contentUrl;
        vc.productID = mg.id_id;
        vc.imageViewStr = mg.coverUrl;
        vc.titleStr = mg.name;
        vc.publishTime = mg.publishTime;
        vc.period = @"-2";
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(void)setCollectionUi
{
    _tableViewCollection = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    _tableViewCollection.delegate = self;
    _tableViewCollection.dataSource = self;
    _tableViewCollection.estimatedRowHeight=0;
    _tableViewCollection.estimatedSectionFooterHeight =0;
    _tableViewCollection.estimatedSectionHeaderHeight =0;
    [_tableViewCollection setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
    [self.view addSubview:_tableViewCollection];
    [self.tableViewCollection registerClass:[LSZDocumentdetailsCell class] forCellReuseIdentifier:@"LSZDocumentdetailsCell"];
    /*
    __weak typeof(self) weakSelf = self;
    
    weakSelf.tableViewCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page = 0;
            [weakSelf getCollectionData:weakSelf.page];
            [weakSelf.tableViewCollection.mj_header endRefreshing];
        });
    }];
    weakSelf.tableViewCollection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.page++;
            [self getCollectionData:self.page];
            [self.tableViewCollection.mj_footer endRefreshing];
        });
    }];
    */
}


@end
